/*
 * Copyright (C) 2011 Project R'yleh Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.Arkham;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class ShopSell extends ListActivity {
	
	
	static String[] ITEMS = new String[GameFile.party.inventory.size()];
    int LongClickChoice;
    String title;
    
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
/*
	    for( int i=0; i<GameFile.party.inventory.size(); i++){
	    	ITEMS[i] = GameFile.party.inventory.get(i).getName();
	    	if ( GameFile.party.inventory.isEquipped(i) )
	    	   ITEMS[i] += " (E) " + GameFile.party.inventory.get(i).getEquippedBy();
	    	ITEMS[i] += " ~ Sells for $" + GameFile.party.inventory.get(i).getSellPrice();
	    }
	    setListAdapter(new ArrayAdapter<String>(this, R.layout.backpack, ITEMS));
*/
	    updateListView();

	    ListView lv = getListView();
	    lv.setTextFilterEnabled(true);
	    
	    lv.setOnItemLongClickListener( new AdapterView.OnItemLongClickListener(){
	    	public boolean onItemLongClick(AdapterView<?> av, View v, int pos, long id) {
	    		
	    	   onLongListItemClick(v,pos,id);
	    	   
	    	   //set to true so that onListItemClick will not activate.
	    	   return true;
	    	}
        }); 
  
	  }
	
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		// Get the item that was clicked
		Object o = this.getListAdapter().getItem(position);
		String keyword = o.toString();
		Toast.makeText(this, keyword, Toast.LENGTH_LONG)
				.show();
		showItem(position);
	}
	
	protected void showItem(int position){
		Intent i;
		i = new Intent(this, ItemDetails.class);
		i.putExtra("item", GameFile.party.inventory.get(position));
		i.putExtra("itemStats", GameFile.party.inventory.get(position).getModifiers());
		startActivity(i);
	}
	
	protected void onLongListItemClick(View v, int pos, long id) {

		LongClickChoice = pos;
		
		//Do you really want to purchase this?
		new AlertDialog.Builder(this)
	    .setTitle("Sell " + GameFile.party.inventory.get(LongClickChoice).getName() + " for $" + 
	    		GameFile.party.inventory.get(LongClickChoice).getSellPrice() + "?")
	    .setItems(R.array.yes_no_array,
	    		new DialogInterface.OnClickListener() {
	    	      public void onClick(DialogInterface dialoginterface, int a) {
	    	    	  if(a == 0 ){
	    	    		  //sell
	    	    		  Toast.makeText(getApplicationContext(), "You sold a " + 
	    	    			   GameFile.party.inventory.get(LongClickChoice).getName() +
	    					   " for " + GameFile.party.inventory.get(LongClickChoice).getSellPrice(), 
	    					   Toast.LENGTH_LONG).show();
	    	    		  GameFile.party.inventory.sellItem(LongClickChoice);
	    	    		  updateListView();
	    	    	  }
	    	    	  else{
	    	    		  //did not sell
	  					Toast.makeText(getApplicationContext(), 
                           "Too bad, I knew someone who could really use it. What else are ya selling?", 
                           Toast.LENGTH_LONG).show();
	    	    	  }
	    	      }
	    })
	    .show();
	}
	
	protected void updateListView(){
		ITEMS = new String[GameFile.party.inventory.size()];
	    for( int i=0; i<GameFile.party.inventory.size(); i++){
	    	ITEMS[i] = GameFile.party.inventory.get(i).getName();
	    	if ( GameFile.party.inventory.isEquipped(i) )
	    	   ITEMS[i] += " (E) " + GameFile.party.inventory.get(i).getEquippedBy();
	    	ITEMS[i] += " ~ Sells for $" + GameFile.party.inventory.get(i).getSellPrice();
	    }
	    setListAdapter(new ArrayAdapter<String>(this, R.layout.backpack, ITEMS));
	    
	    title = "Sell Items                                        ";
	    title += "Wallet: $" + GameFile.party.inventory.getMoney();
	    this.setTitle(title);
	}

}